package com.taotao.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.pojo.UploadPictureResult;
import com.taotao.service.PictureService;
import com.taotao.util.FastDFSClient;

@Service
public class PictureServiceImpl implements PictureService{

	// 从配置文件中取 
	@Value("${IMAGE_SERVICE_BASE_URL}")
	private String IMAGE_SERVICE_BASE_URL;
	
	@Override
	public UploadPictureResult uploadPicture(MultipartFile file) throws Exception {
		UploadPictureResult result = new UploadPictureResult();
		// 判断file 是否为空
		if (file.isEmpty()) {
			result.setError(1);
			result.setMessage("图片为空");
			return result;
		}
		String filename = file.getOriginalFilename();
		// 获取文件的扩展名 不要 “.”
		String extName = filename.substring(filename.lastIndexOf(".")+1);		
		FastDFSClient client=new FastDFSClient("classpath:properties/Client.conf");		
		String url =IMAGE_SERVICE_BASE_URL+client.uploadFile(file.getBytes(), extName);
		//把url响应给客户端
		result.setError(0);
		result.setUrl(url);
		return result;
	}

}
